package com.party.notify.notifyPush.event;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.ProjectWithAuthor;
import com.party.core.model.message.Message;
import com.party.core.model.message.MessageTag;
import com.party.core.model.notify.TargetTemplate;
import com.party.core.model.order.OrderStatus;
import com.party.core.service.member.IPackageCountService;
import com.party.notify.notifyPush.servce.INotifySendService;
import com.party.notify.notifyPush.servce.INotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by wei.li
 *
 * @date 2017/10/31 0031
 * @time 16:51
 */

@Service
public abstract class INotifyEventSendService {

    @Autowired
    private INotifyService notifyService;

    @Autowired
    private INotifySendService notifySendService;

    @Autowired
    private IPackageCountService packageCountService;

    private ExecutorService threadPool = Executors.newFixedThreadPool(500);

    /**
     * 获取众筹列表
     * @return 众筹列表
     */
    public abstract List<ProjectWithAuthor> getList(String eventId);

    /**
     * 事件编号
     * @return 事件编号
     */
    public abstract String eventCode();


    /**
     * 消息推送
     * @param projectWithAuthor 项目编号
     * @param targetTemplate 模板消息
     * @param eventCode 消息编号
     * @param targetId 目标编号
     * @param authorId 会员编号
     * @param way 发送方式
     */
    public void toPush(ProjectWithAuthor projectWithAuthor, TargetTemplate targetTemplate,
                       String eventCode, String targetId, String authorId, String way){
        HashMap<String, Object> constant = Maps.newHashMap();
        String user = projectWithAuthor.getAuthorName() == null ? "" : projectWithAuthor.getAuthorName();
        constant.put("{user}", user);
        constant.put("{project}", projectWithAuthor.getTitle());
        constant.put("{favorerNum}", projectWithAuthor.getFavorerNum());
        constant.put("{actualAmount}", projectWithAuthor.getActualAmount());
        constant.put("logo", Message.MSG_LOGO_ACT);
        constant.put("relId", projectWithAuthor.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("orderStatus", OrderStatus.ORDER_STATUS_HAVE_PAID.getCode());
        constant.put("createBy", projectWithAuthor.getAuthorId());
        constant.put("targetId", Strings.nullToEmpty(targetId));
        constant.put("senderId", Strings.nullToEmpty(authorId));
        constant.put("way", way);
        notifySendService.getProjectPartner(projectWithAuthor.getId(), constant);
        constant.put("templateId", targetTemplate.getId());
        notifyService.push(eventCode, projectWithAuthor.getAuthorMobile(),
                projectWithAuthor.getAuthorId(), constant, targetTemplate.getTemplate());
    }


    /**
     * 发送短信
     * @param targetTemplate 模板消息
     * @param targetId 目标编号
     * @param authorId 会员编号
     * @param way 发送方式
     */
    public boolean send(TargetTemplate targetTemplate, String targetId, String authorId, String way){
        List<ProjectWithAuthor> list = this.getList(targetId);
        boolean isCanFlag = packageCountService.checkSpareNum(targetTemplate, list.size());
        if (isCanFlag) {
            String eventCode = this.eventCode();
            for (ProjectWithAuthor projectWithAuthor : list) {
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        toPush(projectWithAuthor, targetTemplate, eventCode, targetId, authorId, way);
                    }
                });
            }
        }
        return isCanFlag;
    }
}
